package pl.lodz.p.pai.invoice;

import java.util.List;
import java.util.Map;

import javax.xml.datatype.XMLGregorianCalendar;

import pl.lodz.p.pai.common.ErrorsMessages;
import pl.lodz.p.pai.invoice.bean.AddInvoiceStatusBean;
import pl.lodz.p.pai.invoice.db.InvoiceDao;
import pl.lodz.p.pai.invoice.types.AddInvoiceIn;
import pl.lodz.p.pai.invoice.types.AddInvoiceOut;
import pl.lodz.p.pai.invoice.types.GetInvoiceByCustomerIn;
import pl.lodz.p.pai.invoice.types.GetInvoiceByCustomerOut;
import pl.lodz.p.pai.invoice.types.GetInvoiceByDateIn;
import pl.lodz.p.pai.invoice.types.GetInvoiceByDateOut;
import pl.lodz.p.pai.invoice.types.GetInvoiceIn;
import pl.lodz.p.pai.invoice.types.GetInvoiceOut;
import pl.lodz.p.pai.invoice.types.InvoiceFaultDetail;

public class InvoiceManager implements Invoice{
	
	private InvoiceDao invoiceDao;

	public GetInvoiceOut getInvoice(GetInvoiceIn getInvoiceIn) throws InvoiceFault{
		try{
			String invoiceId = getInvoiceIn.getInvoiceId();
			
			if(invoiceId != null){
				pl.lodz.p.pai.invoice.types.Invoice invoice = invoiceDao.getInvoice(invoiceId);
				if(invoice == null){
					throw createErrorMessage(ErrorsMessages.NO_INVOICE_ERROR, "No invoice with id[" + invoiceId + "]");
				}else{
					GetInvoiceOut response = new GetInvoiceOut();
					response.setInvoice(invoice);
					return response;
				}
			}else{
				throw createErrorMessage(ErrorsMessages.NO_INVOICE_ID_ERROR, "Invoice id is null");
			}
		}catch (Exception e) {
			throw createErrorMessage(ErrorsMessages.DB_ERROR, e.toString());
		}
	}

	public GetInvoiceByDateOut getInvoiceByDate(GetInvoiceByDateIn getInvoiceByDateIn) throws InvoiceFault{
		try{
			XMLGregorianCalendar startDate = getInvoiceByDateIn.getStartDate();
			XMLGregorianCalendar endDate = getInvoiceByDateIn.getEndDate();
			
			if(startDate != null && endDate != null){
				List<pl.lodz.p.pai.invoice.types.Invoice> invoiceList = invoiceDao.getInvoiceByDate(startDate, endDate);
				if(invoiceList == null || invoiceList.isEmpty()){
					throw createErrorMessage(ErrorsMessages.NO_INVOICE_ERROR, "No invoices from[" + startDate + "] to[" + endDate + "]");
				}else{
					GetInvoiceByDateOut response = new GetInvoiceByDateOut();
					response.getInvoice().addAll(invoiceList);
					return response;
				}
			}else{
				throw createErrorMessage(ErrorsMessages.WRONG_DATE_ERROR, "Start date[" + startDate + "], end date[" + endDate + "]");
			}
		}catch (Exception e) {
			throw createErrorMessage(ErrorsMessages.DB_ERROR, "");
		}
	}

	public GetInvoiceByCustomerOut getInvoiceByCustomer(GetInvoiceByCustomerIn getInvoiceByCustomerIn) throws InvoiceFault{
		try{
			String customerId = getInvoiceByCustomerIn.getCustomerId();
			
			if(customerId != null){
				List<pl.lodz.p.pai.invoice.types.Invoice> invoiceList = invoiceDao.getInvoiceByCustomer(customerId);
				if(invoiceList == null || invoiceList.isEmpty()){
					throw createErrorMessage(ErrorsMessages.NO_INVOICE_ERROR, "Customer with id[" + customerId + "] has no invoices");
				}else{
					GetInvoiceByCustomerOut response = new GetInvoiceByCustomerOut();
					response.getInvoice().addAll(invoiceList);
					return response;
				}
			}else{
				throw createErrorMessage(ErrorsMessages.NO_CUSTOMER_ID_ERROR, "Customer id is null");
			}
		}catch (Exception e) {
			throw createErrorMessage(ErrorsMessages.DB_ERROR, "");
		}
    }

	public AddInvoiceOut addInvoice(AddInvoiceIn addInvoiceIn) throws InvoiceFault{
		try{
			pl.lodz.p.pai.invoice.types.Invoice invoice = addInvoiceIn.getInvoice();
			
			if(invoice != null){
				AddInvoiceStatusBean statusBean = invoiceDao.addInvoice(invoice);
				if(statusBean != null){
					AddInvoiceOut response = new AddInvoiceOut();
					response.setInvoiceId(statusBean.getInvoiceId());
					response.setStatus(statusBean.getStatus());
					return response;
				}else{
					throw createErrorMessage(ErrorsMessages.DB_ERROR, "");
				}
			}else{
				throw createErrorMessage(ErrorsMessages.NO_CUSTOMER_ID_ERROR, "Customer id is null");
			}
		}catch (Exception e) {
			throw createErrorMessage(ErrorsMessages.DB_ERROR, "");
		}
	}
	
	private InvoiceFault createErrorMessage(String message, String error){
		InvoiceFaultDetail faultDetail = new InvoiceFaultDetail();
		faultDetail.setMessage(message);
		faultDetail.setError(error);
		return new InvoiceFault("", faultDetail);
	}
	
	public InvoiceDao getInvoiceDao() {
		return invoiceDao;
	}

	public void setInvoiceDao(InvoiceDao invoiceDao) {
		this.invoiceDao = invoiceDao;
	}
}
